package com.taotao.manage.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.taotao.commen.EasyUiResult;
import com.taotao.manage.pojo.Item;
import com.taotao.manage.pojo.ItemDesc;
import com.taotao.manage.service.ItemDescService;
import com.taotao.manage.service.ItemService;

/**
 * 商品controller
 * @author DuBo
 *
 */
@Controller
@RequestMapping("item")
public class ItemController {

	@Autowired
	private ItemService itemService;
	
	@Autowired
	private ItemDescService itemDescService;
 
	
	/**
	 * 新增商品信息
	 * @param item
	 * @param desc
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST)
	public ResponseEntity<Void> saveItem(Item item, @RequestParam("desc")String desc){
		try {
			if (StringUtils.isEmpty(desc)) {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			Boolean flag = this.itemService.saveItemAndDesc(item,desc);
			if (flag) {
				return ResponseEntity.status(HttpStatus.CREATED).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	/**
	 * 分页获取商品列表
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="list",method=RequestMethod.GET)
	public ResponseEntity<EasyUiResult> queryPageList(@RequestParam(value="page",defaultValue="1") Integer pageNum,@RequestParam(value="rows",defaultValue="10")Integer pageSize){
		
		try {
			// 设置查询分页条件，状态为1
			Item record = new Item();
			record.setStatus(1);
			PageInfo<Item> pageInfo = this.itemService.queryPageListByWhere(pageNum, pageSize, record);
			
			EasyUiResult result = new EasyUiResult(pageInfo.getTotal(), pageInfo.getList());
			return ResponseEntity.status(HttpStatus.OK).body(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
		
	}
	
	/**
	 * 根据商品id查找商品描述
	 * @param cid
	 * @return
	 */
	@RequestMapping(value="desc/{itemId}",method=RequestMethod.GET)
	public ResponseEntity<ItemDesc> queryDesc(@PathVariable("itemId")Long itemId){
		try {
			ItemDesc desc = this.itemDescService.queryById(itemId);
			return ResponseEntity.ok(desc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	
	
	/**
	 * 更新商品信息
	 * @param item
	 * @param desc
	 * @return
	 */
	@RequestMapping(method=RequestMethod.PUT)
	public ResponseEntity<Void> updateItem(Item item, @RequestParam("desc")String desc){
		try {
			if (item.getId() == null ) {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			Boolean flag = this.itemService.updateItemAndDesc(item,desc);
			if (flag) {
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	
	
	
}
